package com.cl.it.spider;

import com.cl.it.mbg.entity.Company;
import com.cl.it.mbg.entity.ItjuziBusinessChangeInfo;
import com.cl.it.mbg.entity.ItjuziBusinessInfo;
import com.cl.it.mbg.service.IItjuziBusinessChangeInfoService;
import com.cl.it.mbg.service.IItjuziBusinessInfoService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ItJuziSpider extends AbstractSpider{

    @Autowired
    private IItjuziBusinessChangeInfoService businessChangeInfoService;

    @Autowired
    private IItjuziBusinessInfoService businessInfoService;

    protected void executeSingleCompany(WebDriver driver, WebDriverWait wait, Company company) {
        sleep(2000L);
        driver.get(company.getItjuziUrl());
        sleep(2000L);
        businessChange(driver, wait, company);
        log.info("step log => 工商变更提取成功 => {}！", company.getName());

        holderInfo(driver, wait, company);
        log.info("step log => 股东信息提取成功 => {}！", company.getName());
    }

    private void businessChange(WebDriver driver, WebDriverWait wait, Company company) {
        String tabSelector = "div#business div#tab-4";
        if (!has(driver, tabSelector)) {
            return;
        }
        jsClick(driver, tabSelector);
        sleep(1000L);

        String listCssSelector = "div#business div.company-business-wrap div.el-tabs__content div#pane-4 div.el-table__body-wrapper>table tbody tr";
        String pageCssSelector = "div#business div.company-business-wrap div.el-tabs__content div#pane-4 button.btn-next";

        new NextPageSpider() {
            @Override
            public int loopStart() {
                return 0;
            }

            @Override
            public Object one(WebElement element) {
                ItjuziBusinessChangeInfo info = new ItjuziBusinessChangeInfo();
                info.setName(company.getName());
                info.setChangeDate(selectors(element, "td").get(0).getText().trim());
                info.setChangeProject(selectors(element, "td").get(1).getText().trim());
                info.setChangeBefore(selectors(element, "td").get(2).getText().trim());
                info.setChangeAfter(selectors(element, "td").get(3).getText().trim());

                UpdateWrapper<ItjuziBusinessChangeInfo> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("name", info.getName());
                updateWrapper.eq("change_date", info.getChangeDate());

                businessChangeInfoService.saveOrUpdate(info, updateWrapper);
                return info;
            }

            @Override
            public boolean next(SearchContext pagination) {
                if (pagination == null) {
                    return false;
                }
                WebElement element = (WebElement) pagination;
                boolean hasNext = element.isEnabled();
                if (hasNext) {
                    executeScript(driver, "arguments[0].click();", Lists.newArrayList(pagination));
                }
                return hasNext;
            }
        }.each(driver, listCssSelector, pageCssSelector);
    }

    private void holderInfo(WebDriver driver, WebDriverWait wait, Company company) {
        String tabSelector = "div#business div#tab-2";
        if (!has(driver, tabSelector)) {
            return;
        }
        jsClick(driver, tabSelector);
        sleep(1000L);

        String listCssSelector = "div#business div.company-business-wrap div.el-tabs__content div#pane-2 div.el-table__body-wrapper>table tbody tr";
        String pageCssSelector = "div#business div.company-business-wrap div.el-tabs__content div#pane-2 button.btn-next";

        new NextPageSpider() {
            @Override
            public int loopStart() {
                return 0;
            }

            @Override
            public Object one(WebElement element) {
                ItjuziBusinessInfo info = new ItjuziBusinessInfo();
                info.setName(company.getName());
                info.setShareHolder(selectors(element, "td").get(0).getText().trim());
                info.setShareRate(selectors(element, "td").get(1).getText().trim());
                info.setAckPaidAmount(selectors(element, "td").get(2).getText().trim());
                info.setAckPaidDate(selectors(element, "td").get(3).getText().trim());

                UpdateWrapper<ItjuziBusinessInfo> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("name", info.getName());
                updateWrapper.eq("share_holder", info.getShareHolder());

                businessInfoService.saveOrUpdate(info, updateWrapper);
                return info;
            }

            @Override
            public boolean next(SearchContext pagination) {
                if (pagination == null) {
                    return false;
                }
                WebElement element = (WebElement) pagination;
                boolean hasNext = element.isEnabled();
                if (hasNext) {
                    executeScript(driver, "arguments[0].click();", Lists.newArrayList(pagination));
                }
                return hasNext;
            }
        }.each(driver, listCssSelector, pageCssSelector);
    }

    protected void login(WebDriver driver, WebDriverWait wait) {
        driver.get("https://www.itjuzi.com/login");
        sleep(1000L);
        selector(driver, "div.login-form-box form.el-form input[type=text]").sendKeys("17717224389");
        selector(driver, "div.login-form-box form.el-form input[type=password]").sendKeys("y3Kt8H7.wziA7wU");
        sleep(1000L);
        selector(driver, "div.login-form-box button[type=button]").click();
    }
}
